package com.world.cat.controller.sys;

import com.world.cat.model.sys.RoleResource;
import com.world.cat.service.generate.FieldService;
import com.world.cat.service.sys.RoleResourceService;
import com.world.common.pojo.Result;
import com.world.common.pojo.WorldPage;
import com.world.common.util.NumberUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/back/roleResource")
public class RoleResourceController {
    @Resource
    private RoleResourceService service;

    @RequestMapping(value = "/home")
    public String home(Model model) {
        model.addAttribute("fieldMap", FieldService.getFieldMap(RoleResource.class));
        model.addAttribute("qClassActions", FieldService.getClassActionInfo(RoleResource.class));
        return "/back/roleResource/home";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String params, HttpServletRequest request, WorldPage worldPage) throws Exception {
        worldPage.setPageIndex(NumberUtil.intValue(request.getParameter("pagenum")) + 1);
        worldPage.setPageSize(NumberUtil.intValue(request.getParameter("pagesize")));
        worldPage.setProperties("createTime");
        Page<RoleResource> pages = service.findPage(RoleResource.class, params, worldPage.getPageRequest());
        return pages;
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(String params) throws Exception {
        Result result = service.save(RoleResource.class, params);
        return result;
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public Object del(String[] ids) {
        service.del(ids);
        return new Result(true);
    }

    @RequestMapping(value = "/edit")
    public String edit(Model model, String id) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(RoleResource.class));
        RoleResource object = service.get(id);
        model.addAttribute("obj", object);
        return "/back/roleResource/edit";
    }

    @RequestMapping(value = "/add")
    public String add(Model model) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(RoleResource.class));
        return "/back/roleResource/add";
    }

    @RequestMapping(value = "/view")
    public String view(Model model, String id) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(RoleResource.class));
        RoleResource object = service.get(id);
        model.addAttribute("obj", object);
        return "/back/roleResource/view";
    }
}
